package demo.day123;

import Common.DriverCommon;
import Util.CalculateLocation;
import org.neo4j.driver.*;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.types.Relationship;

import java.util.ArrayList;
import java.util.List;

import static org.neo4j.driver.Values.parameters;

/**
 * 查询pitch pitch building的demo
 */
public class RangeDemo {
    public static void main(String[] args) {
        try (DriverCommon driverCommon = new DriverCommon()) {
            int all =0;
            List<List<Node>> lists = new ArrayList<>();
            Driver driver = driverCommon.getGraphDatabase();
            try (Session session = driver.session()) {
                try (Transaction tx = session.beginTransaction()) {
//                    String cypherQuery = "MATCH (n {fclass: 'pitch'})-[:NEAR]-(otherNode {fclass: 'pitch'})-[:NEAR]-(buildingNode {fclass: 'building'})\n" +
//                            "RETURN  n as node1\n" +
//                            " ,otherNode as node2 , buildingNode as builds union MATCH (buildingNode2 {fclass: 'building'})-[:NEAR]-(otherNode2 {fclass: 'pitch'})-[:NEAR]-(n2 {fclass: 'pitch'})\n" +
//                            "RETURN  n2 as node1\n" +
//                            " ,otherNode2 as node2 , buildingNode2 as builds union MATCH (n2 {fclass: 'pitch'})-[:NEAR]-(buildingNode2 {fclass: 'building'})-[:NEAR]-(otherNode2 {fclass: 'pitch'})\n" +
//                            "RETURN  n2 as node1\n" +
//                            " ,otherNode2 as node2 , buildingNode2 as builds";

                    String cypherQuery = "MATCH (n {fclass: 'pitch'})-[r:NEAR]-(otherNode {fclass: 'pitch'}) return n ,otherNode,r";
                    Result result = tx.run(cypherQuery);
                    while (result.hasNext()) {
                        Record record = result.next();
                        Node node1 = record.get("n").asNode();

                        Node otherNode = record.get("otherNode").asNode();

                        Relationship relationship = record.get("r").asRelationship();

                        String location =  relationship.get("location").asString();
                        if(node1 != null && otherNode != null ){
                            if(location.equals("西南")){
                                // 构建第二个查询，基于已找到的节点 node2
                                String secondQuery = "MATCH (otherNode)-[r:NEAR]-(buildingNode2 {fclass: 'building'}) WHERE ID(otherNode) = $otherNodeId RETURN buildingNode2,r";
                                Result secondResult = tx.run(secondQuery,parameters("otherNodeId",otherNode.id() ));
                                while (secondResult.hasNext()) {
                                    Record secondRecord = secondResult.next();
                                    Node buildingNode2 = secondRecord.get("buildingNode2").asNode();
                                    Relationship relationship2 = secondRecord.get("r").asRelationship();
                                   String location2 = relationship2.get("location").asString();
                                   if (location2.equals("东南")){
                                       System.out.println(node1.get("name")+" "+otherNode.get("name")+" "+buildingNode2.get("name"));
                                        all++;
                                   }
                                }
                            }
                        }
                    }
                    System.out.println(all);
                    // 提交事务
                    tx.commit();
                }
            }
            driver.close();
        }
    }
}
